﻿using DevExpress.ExpressApp;
using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using Tida.Module.BusinessObjects;
using DevExpress.ExpressApp.StateMachine.Xpo;
using DevExpress.Data.Filtering;


namespace Tida.Module.BusinessObjects.系统管理
{
    [NonPersistent]
    public class StateHelper
    {



        //获取状态机id
        public XpoStateMachine GetStateMachine(Session session, Type type)
        {


            var stateMachine = new XPQuery<XpoStateMachine>(session).Where(mm => mm.TargetObjectType == type).FirstOrDefault();


            return stateMachine;

        }

        // 获取状态机相关状态
        public XpoState GetCurrtState(String Curr, Session session, Type type)
        {
            var id = GetStateMachine(session, type);

            var sta = new XPQuery<状态>(session).Where(mm => mm.类型 == Curr).FirstOrDefault();
            var statstr = "OA.Module.BusinessObjects.系统管理.状态(" + sta.Oid.ToString() + ")";
            var state = new XPQuery<XpoState>(session).Where(mm => mm.StateMachine == id && mm.MarkerValue == statstr).FirstOrDefault();
            var stat = new XPQuery<XpoState>(session).Where(mm => mm.StateMachine == id).ToList();
            foreach (var item in stat)
            {
                Console.WriteLine(item.MarkerValue);

            }
            return state;

        }
        //声明 要发消息的用户列表

        public List<Sys_User> users = new List<Sys_User>();
        //发送邮件 和弹框提示
        public void SendMsgToNext(object bo, Session session, String Curr)
        {

            Type type = bo.GetType();
            var machine = GetStateMachine(session, type);
            var state = GetCurrtState(Curr, session, type);
            var states = new XPQuery<XpoTransition>(session).Where(mm => mm.SourceState.Oid == state.Oid).ToList();
            foreach (var item in states)
            {


                string option = item.TargetState.TargetObjectCriteria;
                getinfo(option, bo, session);


            }

            if (users.Count > 0)
            {


                foreach (var item in users)
                {
                    var t = bo.GetType().GetProperty("编号");//这里使用反射得到Name这个属性
                    var rt = t.GetValue(bo, null);//然后再得
                    TaskWithNotifications ts = new TaskWithNotifications(session);
                    ts.IsPostponed = false;
                    ts.Subject = type.Name + "待处理";
                    ts.StartDate = System.DateTime.Now;
                    ts.Description = type.Name + rt.ToString() + "待处理";
                    ts.RemindIn = new TimeSpan(0, 5, 0);
                    ts.AlarmTime = System.DateTime.Now;
                    ts.AssignedTo = item;
                    ts.Save();

                    MailHelper.sendMail(item.邮件地址, type.Name + rt.ToString() + "待处理,OA系统自动生成，无需回复~！", type.Name + "待处理");


                }



            }


        }





        //递归获得用户列表

        public void getinfo(string mssg, object obj, Session session)
        {
            if (mssg.Contains("CurrentUserId()"))
            {
                if (mssg.Contains("["))
                {

                    var i = mssg.IndexOf("[");
                    var j = mssg.IndexOf("]");

                    string msg = mssg.Substring(i + 1, j - i - 1);
                    getinfos(msg, obj, session);

                    string os = mssg.Remove(i, j - i + 1);
                    var k = os.IndexOf("CurrentUserId()");
                    var l = "CurrentUserId()".Length;
                    string oo = os.Remove(k, l);

                    getinfo(oo, obj, session);


                }
            }
            else if (mssg.Contains("IsCurrentUserInRole"))
            {

                if (mssg.Contains("("))
                {
                    var i = mssg.IndexOf("(");
                    var j = mssg.IndexOf(")");

                    string msg = mssg.Substring(i + 2, j - i - 3);
                    getinfos(msg, obj, session);
                    string os = mssg.Remove(i, j - i + 1);

                    var k = os.IndexOf("IsCurrentUserInRole");
                    var l = "IsCurrentUserInRole".Length;
                    string oo = os.Remove(k, l);

                    getinfo(oo, obj, session);

                }

            }

        }

        public void getinfos(string msg, object obj, Session session)
        {
            if (msg.Contains("."))
            {
                string ss = msg.Substring(0, msg.IndexOf("."));
                // Console.WriteLine(ss.ToString());
                try
                {
                    var t = obj.GetType().GetProperty(ss);//这里使用反射得到Name这个属性

                    if (t == null)
                    {
                        var o = obj.GetType().GetField(ss);
                        var re = o.GetValue(obj);
                        string sss = msg.Substring(msg.IndexOf(".") + 1);
                        getinfos(sss, re, session);
                    }
                    else
                    {
                        var result = t.GetValue(obj, null);//然后再得
                        string sss = msg.Substring(msg.IndexOf(".") + 1);
                        getinfos(sss, result, session);
                    }
                    //var o = result.GetType().GetProperty("Color");
                    //var ss = o.GetValue(result, null);




                }
                catch
                {



                }


            }
            else if (msg.Contains("Oid"))
            {
                //   var t = obj.GetType().GetProperty(msg);//这里使用反射得到Name这个属性
                // var result = t.GetValue(obj, null).ToString();//然后再得
                // var os = Guid.Parse(result);
                //1F5356B7-4C29-405E-9768-3AAB4801B587
                var oo = (Sys_User)obj;
                var u = new XPQuery<Sys_User>(session);
                var us = u.Where(mm => mm == oo).FirstOrDefault();
                if (users.Count(mm => mm.UserName == us.UserName) > 0)
                    return;
                users.Add(us);


            }
            else
            {


                var r = new XPQuery<Sys_Role>(session);
                var rst = r.Where(mm => mm.Name == msg).FirstOrDefault();
                foreach (var xx in rst.Sys_Users)
                {
                    if (users.Count(mm => mm.UserName == xx.UserName) > 0)
                        return;
                    users.Add(xx);

                }



            }
        }



    }
}
